<?php
declare(strict_types=1);

namespace Newbie;


use think\Validate;

/**
 * @method isAjax()
 */
class NewbieFormRequest {
    /**
     * Get custom messages for validator errors.
     */
    public function messages (): array {
        return [];
    }

    /**
     * Get custom attributes for validator errors.
     */
    public function attributes (): array {
        return [];
    }

    /**
     * The scenes defined by developer.
     */
    protected array $scenes = [];

    /**
     * 公共规则
     * @return array
     */
    public function commonRules (): array {
        return [];
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules (): array {
        return [];
    }

    public function fields (): array {
        return [
            'username' => '用户名'
        ];
    }

    public function validated ($scene = '') {
        $param = request()->all();
        (new Validate())->rule($this->rules())->batch(false)->message($this->messages())->scene($scene)->failException()->check($param);
        return $param;
    }

    public function input ($name, $default = '') {
        $param = $this->validated();
        return $param[$name] ?? $default;
    }

    public function all () {
        return $this->validated();
    }

    public function file ($name = null) {
        return request()->file($name);
    }

    public function __call ($method, $args = []) {
        return request()->$method($args);
    }
}